<script lang="ts" setup>
import { reactive, ref } from "vue";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { userLoginService, userRegisterService } from "@/api/user";
import { useTokenStore } from "@/stores/token";
import router from "@/router";
import $ from "jquery";
import { useInfoStore } from "@/stores/userInfo";

const ruleFormRef = ref<FormInstance>();

const checkAge = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error("账号不能为空"));
  } else {
    callback();
  }
};

$(function () {
            $('#password').focus(function () {
                // 密码框获得焦点，追加样式.password
                $('#owl').addClass('password');
            }).blur(function () {
                // 密码框失去焦点，移除样式.password
                $('#owl').removeClass('password');
            })
            $('#checkPassword').focus(function () {
                // 密码框获得焦点，追加样式.password
                $('#owl').addClass('password');
            }).blur(function () {
                // 密码框失去焦点，移除样式.password
                $('#owl').removeClass('password');
            })
        })

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("新密码不能为空"));
  } else {
    if (ruleForm.value.checkPassword !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass");
    }
    callback();
  }
};
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请确认新密码"));
  } else if (value !== ruleForm.value.password) {
    callback(new Error("两次密码输入不一致"));
  } else {
    callback();
  }
};

const ruleForm = ref({
  account: "",
  password: "",
  checkPassword: "",
});

const rules = reactive<FormRules<typeof ruleForm>>({
  password: [{ validator: validatePass, trigger: "blur" }],
  checkPassword: [{ validator: validatePass2, trigger: "blur" }],
  account: [{ validator: checkAge, trigger: "blur" }],
});

//清空表单
const resetForm = () => {
  ruleFormRef.value.resetFields();
};

let title = ref("登录");

//登录函数
const userLogin =  () => {
  ruleFormRef.value.validate(async valid=>{
      let result = await userLoginService(ruleForm.value);

      useTokenStore().setToken(result.data);
      router.push("/")

      ElMessage.success("登录成功");
  })
};

//注册函数
const userRegister= async ()=>{
  ruleFormRef.value.validate(async valid=>{
    if(valid){
      let result = await userRegisterService(ruleForm.value);
      ElMessage.success(result.data);

      ruleForm.value.password='';
      title.value='登录';
    }else{
      ElMessage.error("请规范填写")
    }
  })

}
</script>

<template>
  <div id="body">
    <div style="position: fixed; top: -100px">
      <img src="../assets/224022NTvOMfJQGiRAtR6c.png" alt="" />
    </div>
    <div class="login-box" style="margin-top: 130px">
      <div class="owl" id="owl">
        <div class="hand"></div>
        <div class="hand hand-r"></div>
        <div class="arms">
          <div class="arm"></div>
          <div class="arm arm-r"></div>
        </div>
      </div>
      <div class="input-box">
        <el-form
          ref="ruleFormRef"
          style="max-width: 600px"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="auto"
          class="demo-ruleForm"
        >
          <el-form-item prop="account">
            <el-input
              v-model="ruleForm.account"
              class="input"
              placeholder="请输入账号"
            /> </el-form-item
          ><br />
          <el-form-item prop="password">
            <el-input
              v-model="ruleForm.password"
              type="Password"
              autocomplete="off"
              class="input"
              id="password"
              placeholder="请输入密码"
            /> </el-form-item
          ><br />
          <el-form-item prop="checkPassword" v-show="title == '注册'" class="input">
            <el-input
              v-model="ruleForm.checkPassword"
              type="password"
              autocomplete="off"
              id="checkPassword"
              placeholder="请再次输入密码"
            /> </el-form-item
          ><br v-show="title == '注册'" class="input" />
        </el-form>
        <!-- <button @click="title == '登录' ? userLogin() : userRegister()">
          {{ title }}
        </button> -->
        <button
          class="b"
          @click="title == '登录' ? userLogin() : userRegister()"
          style="margin: auto"
        >
          <p>{{ title }}</p>
        </button>
        <br />
        <div
          @click="
            resetForm();
            title == '登录' ? (title = '注册') : (title = '登录');
          "
        >
          <button class="cta">
            <span class="hover-underline-animation">
              {{ title == "登录" ? "前去注册" : "返回登录" }}
            </span>
            <svg
              id="arrow-horizontal"
              xmlns="http://www.w3.org/2000/svg"
              width="30"
              height="10"
              viewBox="0 0 46 16"
            >
              <path
                id="Path_10"
                data-name="Path 10"
                d="M8,0,6.545,1.455l5.506,5.506H-30V9.039H12.052L6.545,14.545,8,16l8-8Z"
                transform="translate(30)"
              ></path>
            </svg>
          </button>
        </div>

        <!-- <div>
          <el-link
            class="mx-1"
            type="primary"
            @click="
              resetForm();
              title == '登录' ? (title = '注册') : (title = '登录');
            "
            >{{ title == "登录" ? "前去注册>>" : "前去登录>>" }}</el-link
          >
        </div> -->
      </div>
    </div>
  </div>
</template>

<style scoped>
* {
  /* 初始化 */
  margin: 0;
  padding: 0;
}

/* 按钮样式 */
.b {
  border: none;
  width: 320px;
  height: 50px;
  color: #fff;
  z-index: 1;
  display: flex;
  background: #000;
  position: relative;
  cursor: pointer;
}

.b p {
  margin: 0 auto;
  align-self: center;
  font-size: 17px;
  font-weight: bold;
  text-align: center;
}

.b::after {
  position: absolute;
  content: "";
  width: 100%;
  z-index: -1;
  height: 10%;
  bottom: 0;
  clip-path: polygon(
    0% 74%,
    4% 75%,
    8% 76%,
    11% 77%,
    15% 78%,
    20% 78%,
    25% 77%,
    32% 77%,
    37% 75%,
    40% 74%,
    43% 74%,
    46% 73%,
    52% 72%,
    57% 72%,
    65% 74%,
    66% 75%,
    71% 78%,
    75% 82%,
    81% 86%,
    83% 88%,
    88% 91%,
    90% 94%,
    94% 96%,
    98% 98%,
    100% 100%,
    82% 100%,
    0 100%
  );
  background: #8792eb;
  transition: 0.2s ease;
}

.b::before {
  position: absolute;
  content: "";
  /*   bottom: 80%; */
  transform: rotate(180deg);
  width: 100%;
  height: 10%;
  transition: 0.2s ease;
  /*   bottom:; */
  z-index: -1;
  clip-path: polygon(
    0% 74%,
    4% 75%,
    8% 76%,
    11% 77%,
    15% 78%,
    20% 78%,
    25% 77%,
    32% 77%,
    37% 75%,
    40% 74%,
    43% 74%,
    46% 73%,
    52% 72%,
    57% 72%,
    65% 74%,
    66% 75%,
    71% 78%,
    75% 82%,
    81% 86%,
    83% 88%,
    88% 91%,
    90% 94%,
    94% 96%,
    98% 98%,
    100% 100%,
    82% 100%,
    0 100%
  );
  background: #8792eb;
}

.b:hover::after {
  clip-path: polygon(
    0 30%,
    9% 34%,
    7% 39%,
    11% 43%,
    13% 33%,
    17% 30%,
    24% 34%,
    25% 35%,
    30% 31%,
    30% 38%,
    39% 33%,
    35% 43%,
    43% 45%,
    55% 46%,
    65% 74%,
    67% 66%,
    81% 57%,
    75% 82%,
    81% 86%,
    83% 88%,
    88% 91%,
    90% 94%,
    94% 96%,
    98% 98%,
    100% 100%,
    82% 100%,
    0 100%
  );
  height: 80%;
}

.b:hover::before {
  clip-path: polygon(
    0 30%,
    9% 34%,
    7% 39%,
    11% 43%,
    13% 33%,
    17% 30%,
    24% 34%,
    25% 35%,
    30% 31%,
    30% 38%,
    39% 33%,
    35% 43%,
    43% 45%,
    55% 46%,
    65% 74%,
    67% 66%,
    81% 57%,
    75% 82%,
    81% 86%,
    83% 88%,
    88% 91%,
    90% 94%,
    94% 96%,
    98% 98%,
    100% 100%,
    82% 100%,
    0 100%
  );
  height: 80%;
}
/* 登录注册切换样式 */
.cta {
  border: none;
  background: none;
  cursor: pointer;
}

.cta span {
  padding-bottom: 7px;
  letter-spacing: 4px;
  font-size: 14px;
  padding-right: 15px;
  text-transform: uppercase;
}

.cta svg {
  transform: translateX(-8px);
  transition: all 0.3s ease;
}

.cta:hover svg {
  transform: translateX(0);
}

.cta:active svg {
  transform: scale(0.9);
}

.hover-underline-animation {
  position: relative;
  color: black;
  padding-bottom: 20px;
}

.hover-underline-animation:after {
  content: "";
  position: absolute;
  width: 100%;
  transform: scaleX(0);
  height: 2px;
  bottom: 0;
  left: 0;
  background-color: #000000;
  transform-origin: bottom right;
  transition: transform 0.25s ease-out;
}

.cta:hover .hover-underline-animation:after {
  transform: scaleX(1);
  transform-origin: bottom left;
}

#body {
  /* 100%窗口高度 */
  height: 100vh;
  /* 弹性布局 居中 */
  display: flex;
  justify-content: center;
  align-items: center;
  /* 渐变背景 */
  background: linear-gradient(200deg, #72afd3, #96fbc4);
}

.login-box {
  /* 相对定位 */
  position: relative;
  width: 320px;
}

.input-box {
  /* 弹性布局 垂直排列 */
  display: flex;
  flex-direction: column;
}

.input {
  height: 40px;
  border-radius: 3px;
  /* 缩进15像素 */
  text-indent: 15px;
  outline: none;
  border: none;
}

.input:focus {
  outline: 1px solid lightseagreen;
}
/*
.input-box button {
  border: none;
  height: 45px;
  background-color: lightseagreen;
  color: #fff;
  border-radius: 3px;
  cursor: pointer;
}*/

/* 接下来是猫头鹰的样式 */
.owl {
  width: 211px;
  height: 108px;
  /* 背景图片 */
  background: url("/src/assets/owl-login.png") no-repeat;
  /* 绝对定位 */
  position: absolute;
  top: -100px;
  /* 水平居中 */
  left: 50%;
  transform: translateX(-50%);
}

.owl .hand {
  width: 34px;
  height: 34px;
  border-radius: 40px;
  background-color: #472d20;
  /* 绝对定位 */
  position: absolute;
  left: 12px;
  bottom: -8px;
  /* 沿Y轴缩放0.6倍（压扁） */
  transform: scaleY(0.6);
  /* 动画过渡 */
  transition: 0.3s ease-out;
}

.owl .hand.hand-r {
  left: 170px;
}

.owl.password .hand {
  transform: translateX(42px) translateY(-15px) scale(0.7);
}

.owl.password .hand.hand-r {
  transform: translateX(-42px) translateY(-15px) scale(0.7);
}

.owl .arms {
  position: absolute;
  top: 58px;
  width: 100%;
  height: 41px;
  overflow: hidden;
}

.owl .arms .arm {
  width: 40px;
  height: 65px;
  position: absolute;
  left: 20px;
  top: 40px;
  background: url("../images/owl-login-arm.png") no-repeat;
  transform: rotate(-20deg);
  transition: 0.3s ease-out;
}

.owl .arms .arm.arm-r {
  transform: rotate(20deg) scaleX(-1);
  left: 158px;
}

.owl.password .arms .arm {
  transform: translateY(-40px) translateX(40px);
}

.owl.password .arms .arm.arm-r {
  transform: translateY(-40px) translateX(-40px) scaleX(-1);
}
</style>
